package net.jueb.frameworkDemo.orm.mysql.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.jueb.frameworkDemo.orm.enums.RoleSexEnum;
import net.jueb.frameworkDemo.orm.enums.RoleStatusEnum;

public class MybatisUtil {

	protected final static Logger _log = LoggerFactory.getLogger(MybatisUtil.class);

	private final static SqlSessionFactory sqlSessionFactory;
	static {
		InputStream in=MybatisUtil.class.getResourceAsStream("mybatis-config.xml");
		InputStream dbin=MybatisUtil.class.getResourceAsStream("db.properties");
		Properties ps=new Properties();
		try {
			ps.load(dbin);
		} catch (IOException e) {
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(in, ps);
		//配置类型映射handler
		Configuration cfg = sqlSessionFactory.getConfiguration();
		TypeHandlerRegistry tr = cfg.getTypeHandlerRegistry();
		tr.register(new EnumTypeHandler<RoleSexEnum>(RoleSexEnum.class));
		tr.register(new EnumTypeHandler<RoleStatusEnum>(RoleStatusEnum.class));
	}

	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
}
